package com.java47.controller;

import com.java47.pojo.Flower;
import com.java47.service.FlowerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FlowerController {

    @Resource
    private FlowerService fs;

    @RequestMapping("list")
    public String list(Model model,String index){
        if(index==null||index.equals("")){
            index="1";
        }
        model.addAttribute("str","花卉展示");
        model.addAttribute("flist",fs.showPage(Integer.parseInt(index),3));
        return "show";
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("add")
    public String add(Flower f){
        //执行新增操作
        Map<String,Object> map = new HashMap<>();
        map.put("add",f);
        fs.update(map);
        return "forward:list";
    }

    @RequestMapping("del")
    public String del(String fid){
        //执行删除操作
        Map<String,Object> map = new HashMap<>();
        map.put("del",Integer.parseInt(fid));
        fs.update(map);
        return "forward:list";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model model,String fid){
        //把数据传过去
        model.addAttribute("f",fs.selectOne(Integer.parseInt(fid)));
        return "update";
    }

    @RequestMapping("update")
    public String update(Flower f){
        //执行修改操作
        Map<String,Object> map = new HashMap<>();
        map.put("update",f);
        fs.update(map);
        return "forward:list";
    }



    @RequestMapping("select")
    public String select(Model model,Flower flower){
        model.addAttribute("list",fs.select(flower));
        System.out.println("========="+flower);
        return "test1";
    }

}
